package com.nengjiang.wsplayer;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.nengjiang.wsplayer.oksocket.OkManager;
import com.nengjiang.wsplayer.utils.SPConfig;
import com.nengjiang.wsplayer.utils.SPUtils;
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.player.SystemPlayerManager;

import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;

public class App extends Application {
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        SPUtils.init(context);
        initServerUrl();
        OkManager.getInstance().init();
        initGSYVideoPlayer();
    }

    private void initGSYVideoPlayer() {
        //EXOPlayer内核，支持格式更多
        PlayerFactory.setPlayManager(Exo2PlayerManager.class);
        //系统内核模式
        PlayerFactory.setPlayManager(SystemPlayerManager.class);
        //ijk内核，默认模式
        PlayerFactory.setPlayManager(IjkPlayerManager.class);
    }

    public static Context getAppContext() {
        return context;
    }


    /**
     * 初始化服务器地址，必须放在OkManager初始化之前
     */
    private static void initServerUrl() {
        String socketUrl = SPUtils.getInstance().getString(SPConfig.SP_KEY_SOCKET_URL);
        if (!TextUtils.isEmpty(socketUrl)) {
            Config.SOCKET_URL = socketUrl;
        }

        int socketPort = SPUtils.getInstance().getInt(SPConfig.SP_KEY_SOCKET_PORT);
        if (socketPort != -1) {
            Config.SOCKET_PORT = socketPort;
        }

        String rtspUrl = SPUtils.getInstance().getString(SPConfig.SP_KEY_RTSP_PUSH_URL);
        if (!TextUtils.isEmpty(rtspUrl)) {
            Config.RTSP_PUSH_URL = rtspUrl;
        }
    }
}
